package com.light.boot_demo.listener.service;

import com.light.boot_demo.event.service.SimpleServiceEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
@Slf4j
public class SimpleServiceListener {

    @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
    public void afterRollBack(SimpleServiceEvent simpleServiceEvent) {
        log.error("TransactionPhase.AFTER_ROLLBACK:" + simpleServiceEvent.getMsg());
    }

    //回调方法 在最后 aop之后回调
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
    public void beforeCommit(SimpleServiceEvent simpleServiceEvent) {
        log.info("TransactionPhase.BEFORE_COMMIT:" + simpleServiceEvent.getMsg());
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void afterCommit(SimpleServiceEvent simpleServiceEvent) {
        log.info("TransactionPhase.AFTER_COMMIT:" + simpleServiceEvent.getMsg());
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION)
    public void afterCompletion(SimpleServiceEvent simpleServiceEvent) {
        log.info("TransactionPhase.AFTER_COMPLETION:" + simpleServiceEvent.getMsg());
    }

    @EventListener
    public void sayRol11l(SimpleServiceEvent simpleServiceEvent) {
        log.error("SimpleServiceEvent Eve--------------------------");
    }
}
